package jsonmap

import (
	"gitee.com/zhongguo168a/gocodes/datax/convertx"
	"gitee.com/zhongguo168a/gocodes/datax/schemax/basickind"
	"reflect"
	"strconv"
	"strings"
)

func InterfaceToJSONKey(ival interface{}) string {
	val := ""
	switch x := ival.(type) {
	case string:
		val = x
	case int:
		val = strconv.Itoa(x)
	case int8:
		val = strconv.Itoa(int(x))
	case int16:
		val = strconv.Itoa(int(x))
	case int32:
		val = strconv.Itoa(int(x))
	case int64:
		val = strconv.Itoa(int(x))
	case uint:
		val = strconv.Itoa(int(x))
	case uint8:
		val = strconv.Itoa(int(x))
	case uint16:
		val = strconv.Itoa(int(x))
	case uint32:
		val = strconv.Itoa(int(x))
	case uint64:
		val = strconv.Itoa(int(x))
	case float32:
		val = strconv.Itoa(int(x))
	case float64:
		val = strconv.Itoa(int(x))
	}
	return val
}

func InterfaceToJSONValue(val interface{}) (r interface{}) {
	switch v := val.(type) {
	case string:
		r = v
	case bool:
		r = v
	case int:
		r = v
	case int8:
		r = int(v)
	case int16:
		r = int(v)
	case int32:
		r = int(v)
	case int64:
		r = int(v)
	case uint:
		r = v
	case uint8:
		r = uint(v)
	case uint16:
		r = uint(v)
	case uint32:
		r = uint(v)
	case uint64:
		r = uint(v)
	case float32:
		r = float64(v)
	case float64:
		r = float64(v)
	default:
		r = val
	}
	return
}

// 把目标值，转换成jsonmap定义的类型
func ConvertJsonKeyToReflectKind(kind reflect.Kind, val interface{}) (r interface{}) {
	switch kind {
	case reflect.Bool:
		r = convertx.AnyToBool(val)
	case reflect.Int:
		r = int(convertx.AnyToInt64(val))
	case reflect.Int8:
		r = int8(convertx.AnyToInt64(val))
	case reflect.Int16:
		r = int16(convertx.AnyToInt64(val))
	case reflect.Int32:
		r = int32(convertx.AnyToInt64(val))
	case reflect.Int64:
		r = int64(convertx.AnyToInt64(val))
	case reflect.Uint:
		r = uint(convertx.AnyToInt64(val))
	case reflect.Uint8:
		r = uint8(convertx.AnyToInt64(val))
	case reflect.Uint16:
		r = uint16(convertx.AnyToInt64(val))
	case reflect.Uint32:
		r = uint32(convertx.AnyToInt64(val))
	case reflect.Uint64:
		r = uint64(convertx.AnyToInt64(val))
	case reflect.Float32:
		r = float32(convertx.AnyToFloat64(val))
	case reflect.Float64:
		r = convertx.AnyToFloat64(val)
	case reflect.String:
		r = convertx.AnyToString(val)
	}
	return
}

// ConvertBasicToJsonValue 把目标值，转换成jsonmap定义的类型
func ConvertBasicToJsonValue(kind basickind.Kind, val interface{}) (r interface{}) {
	switch kind {
	case basickind.Bool:
		r = convertx.AnyToBool(val)
	case basickind.Int8:
		fallthrough
	case basickind.Int16:
		fallthrough
	case basickind.Int32:
		fallthrough
	case basickind.Int64:
		r = convertx.AnyToInt(val)
	case basickind.Uint8:
		fallthrough
	case basickind.Uint16:
		fallthrough
	case basickind.Uint32:
		fallthrough
	case basickind.Uint64:
		r = convertx.AnyToUint(val)
	case basickind.Float32:
		fallthrough
	case basickind.Float64:
		r = convertx.AnyToFloat64(val)
	case basickind.String:
		r = convertx.AnyToString(val)
	}
	return
}

func RemoveNil(m map[string]interface{}) {
	removeNil(m)
}

func removeNil(m map[string]interface{}) {
	for key, val := range m {
		//fmt.Printf("remove name=%v, %T\n", key, val)
		switch v := val.(type) {
		case map[string]interface{}:
			if v == nil {
				delete(m, key)
			} else {
				removeNil(v)
			}
		case []interface{}:
			if v == nil {
				delete(m, key)
			} else {
				if len(v) == 0 {
					delete(m, key)
				} else {
					for i := 0; i < len(v); i++ {
						iitem := v[i]
						item, ok := iitem.(map[string]interface{})
						if ok {
							removeNil(item)
						}
					}
				}
			}
		case string:
			if v == "" {
				delete(m, key)
			}
		case interface{}:
			if v == nil {
				delete(m, key)
			}
		default:
			if v == nil {
				//fmt.Println("delete default", key)
				delete(m, key)
			}
		}
	}
}

func parseTag(val string) (r map[string]string) {
	r = map[string]string{}
	arr := strings.Split(val, " ")
	for _, arrval := range arr {
		if strings.HasPrefix("--", arrval) {
			paramsarr := strings.Split(arrval, "=")
			if len(paramsarr) == 1 {
				r[paramsarr[0]] = "true"
			} else {
				r[paramsarr[0]] = r[paramsarr[1]]
			}

		}
	}
	return
}
